# -*- coding: utf-8 -*-
# @Author  : Adolf  Frank
# @Time    : 2021-12-23 08:47


# 将class转dict,以_开头的属性不要
def trans_bean_to_dict(obj):
    print(str(obj))
    pr = {}
    for name in dir(obj):
        value = getattr(obj, name)
        if not name.startswith('__') and not callable(value) and not name.startswith('_'):
            pr[name] = value
    return pr
# 将class转dict,以_开头的也要
def trans_bean_to_dict_with_(obj):

    pr = {}
    for name in dir(obj):
        value = getattr(obj, name)
        if not name.startswith('__') and not callable(value):
            pr[name] = value
    return pr
# dict转obj，先初始化一个obj
def dict2obj(obj,dict):
    obj.__dict__.update(dict)
    return obj

# 将指定Bean转化为dict
# MeterieltrendItem
def trans_meteriel_bean_to_dict(meteriel_bean):
    if meteriel_bean is None:
        return {}

    dict_body = {}
    # 与mapping 保持一致顺序
    dict_body['url'] = meteriel_bean['url']
    dict_body['title'] = meteriel_bean['title']
    dict_body['public_date'] = meteriel_bean['public_date']
    dict_body['region'] = meteriel_bean['region']
    dict_body['market'] = meteriel_bean['market']
    dict_body['product_name'] = meteriel_bean['product_name']
    dict_body['material'] = meteriel_bean['material']
    dict_body['price_970'] = meteriel_bean['price_970']
    dict_body['specification_970'] = meteriel_bean['specification_970']
    dict_body['price_980'] = meteriel_bean['price_980']
    dict_body['specification_980'] = meteriel_bean['specification_980']
    dict_body['comp_unit'] = meteriel_bean['comp_unit']
    dict_body['price_unit'] = meteriel_bean['price_unit']
    dict_body['product_area'] = meteriel_bean['product_area']
    dict_body['manufacturer'] = meteriel_bean['manufacturer']
    dict_body['price_type'] = meteriel_bean['price_type']
    dict_body['price_description'] = meteriel_bean['price_description']
    dict_body['remarks'] = meteriel_bean['remarks']

    dict_body['init_html'] = meteriel_bean['init_html']

    return dict_body

# CopperBarItem
def trans_copperbar_bean_to_dict(meteriel_bean):
    if meteriel_bean is None:
        return {}

    dict_body = {}
    # 与mapping 保持一致顺序
    dict_body['url'] = meteriel_bean['url']
    dict_body['title'] = meteriel_bean['title']
    dict_body['product_name'] = meteriel_bean['product_name']
    dict_body['public_date'] = meteriel_bean['public_date']
    dict_body['market'] = meteriel_bean['market']
    dict_body['specification'] = meteriel_bean['specification']
    dict_body['material'] = meteriel_bean['material']
    dict_body['price_down'] = meteriel_bean['price_down']
    dict_body['price_up'] = meteriel_bean['price_up']
    dict_body['price_average'] = meteriel_bean['price_average']
    dict_body['price_unit'] = meteriel_bean['price_unit']
    dict_body['price_description'] = meteriel_bean['price_description']
    dict_body['remarks'] = meteriel_bean['remarks']
    dict_body['init_html'] = meteriel_bean['init_html']

    return dict_body

# AluminaItem
def trans_alumina_bean_to_dict(meteriel_bean):
    if meteriel_bean is None:
        return {}

    dict_body = {}
    # 与mapping 保持一致顺序
    dict_body['url'] = meteriel_bean['url']
    dict_body['title'] = meteriel_bean['title']
    dict_body['public_date'] = meteriel_bean['public_date']
    dict_body['product_name'] = meteriel_bean['product_name']
    dict_body['price_down'] = meteriel_bean['price_down']
    dict_body['price_up'] = meteriel_bean['price_up']
    dict_body['price_average'] = meteriel_bean['price_average']
    dict_body['market'] = meteriel_bean['market']
    dict_body['price_description'] = meteriel_bean['price_description']
    dict_body['price_unit'] = meteriel_bean['price_unit']
    dict_body['remarks'] = meteriel_bean['remarks']
    dict_body['init_html'] = meteriel_bean['init_html']

    return dict_body

# steel scrap
def trans_steel_bean_to_dict(meteriel_bean):
    if meteriel_bean is None:
        return {}

    dict_body = {}
    # 与mapping 保持一致顺序
    dict_body['url'] = meteriel_bean['url']
    dict_body['title'] = meteriel_bean['title']
    dict_body['release_date'] = meteriel_bean['release_date']
    dict_body['product_name'] = meteriel_bean['product_name']
    dict_body['market'] = meteriel_bean['market']
    dict_body['city_name'] = meteriel_bean['city_name']
    dict_body['price_down'] = meteriel_bean['price_down']
    dict_body['price_up'] = meteriel_bean['price_up']
    dict_body['price_average'] = meteriel_bean['price_average']
    dict_body['price_unit'] = meteriel_bean['price_unit']
    dict_body['price_description'] = meteriel_bean['price_description']
    dict_body['price_type'] = meteriel_bean['price_type']
    dict_body['remarks'] = meteriel_bean['remarks']
    dict_body['init_html'] = meteriel_bean['init_html']

    return dict_body

# new clean technica
def trans_news_bean_to_dict(meteriel_bean):
    if meteriel_bean is None:
        return {}

    dict_body = {}
    # 与mapping 保持一致顺序
    dict_body['url'] = meteriel_bean['url']
    dict_body['title'] = meteriel_bean['title']
    dict_body['release_date'] = meteriel_bean['release_date']
    dict_body['content'] = meteriel_bean['content']
    dict_body['industry'] = meteriel_bean['industry']
    dict_body['tags'] = meteriel_bean['tags']
    dict_body['author_info'] = meteriel_bean['author_info']
    dict_body['remarks'] = meteriel_bean['remarks']
    dict_body['init_html'] = meteriel_bean['init_html']
    dict_body['area_attr'] = meteriel_bean['area_attr']
    dict_body['company_attr'] = meteriel_bean['company_attr']
    dict_body['posi_nega_attr'] = meteriel_bean['posi_nega_attr']

    return dict_body

if __name__ == '__main__':
    str = 'http://chinaccm.cn/PriceInfo/InfoView.aspx?id=222001_2949504'
    print("save_file-----")
    website_name = '/Users/adolf/PycharmProjects/MeterielTrend/data/'
    with open(website_name + str.replace('/', '//'), 'wb') as f:
        f.write("没有经历过挫折的失败，他就不可能成功")

    pass